/**Eureka+Spring-cloud-loadbalance+spring-cloud-gateway:是客户端调用服务的入口，主要功能是将不同的请求转发至不同的处理微服务模块，通常要集成负载均衡服和熔断，
 * 1）依赖库：SpringCloud版本为Hoxton及以下，核心依赖为spring-cloud-starter-netflix-eureka-client（含ribbon
 *          spring-cloud-starter-loadbalancer、和spring-cloud-starter-gateway
 * 2）main上@EnableDiscoveryClient
 * 3）在YAML或main文件中定义负载均衡策略和相关属性
 * 4）在YAML或main文件中定义路由及其predicate、filter
 */
package com.example.demogatewayloadbalancer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RandomLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

@SpringBootApplication
@EnableDiscoveryClient
public class DemoGatewayLoadBalancerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoGatewayLoadBalancerApplication.class, args);
    }

    @Bean
    public ReactiveLoadBalancer<ServiceInstance> reactorServiceInstanceLoadBalancer(Environment environment,
                                                                                    LoadBalancerClientFactory loadBalancerClientFactory) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new RandomLoadBalancer(
                loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);
    }
}
